/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Bucle de juego
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import nme.display.Bitmap;
import nme.Assets;
import nme.ui.Keyboard;
import nme.events.KeyboardEvent;
import nme.events.MouseEvent;
import flash.events.Event;
import flash.Lib;
import nme.display.StageAlign;
import nme.display.StageScaleMode;

class PauseState extends GameState {

  public static var _instance:PauseState;
  private var _logo:Bitmap;

  private function new () {
    super();
    _logo = new Bitmap(Assets.getBitmapData("assets/pausa.png"));
  }

  // Patrón Singleton.
  public static function getInstance() : PauseState {
    if (PauseState._instance == null)
      PauseState._instance = new PauseState();
    return PauseState._instance;
  }

  override function enter () : Void { 
    Lib.current.stage.align = StageAlign.TOP_LEFT;
    Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
    // Al entrar añade el logo para que lo renderice.
    addChild(_logo);
  }

  override function exit() : Void { 
    removeChild(_logo);
  }

  override function pause() : Void { }
  override function resume() : Void { }

  override function keyPressed (event:KeyboardEvent) : Void { 
    // Al estado anterior... (PlayState)
    if (event.keyCode == Keyboard.SPACE) {
      popState();
    }
    // Si pulsamos la tecla r pintamos de nuevo todos los círculos 
	else if (event.keyCode == 82) {
      		for (circle in PlayState.getInstance().getCircles()) {
			circle.fillInBlack();
		}
   	}
  }

  override function keyReleased (event:KeyboardEvent) : Void { }

  override function mouseClicked (event:MouseEvent) : Void {
    var x:Float = event.stageX;
    var y:Float = event.stageY;

    // ¿Colisión sobre algún círculo?
    for (circle in PlayState.getInstance().getCircles()) {
      if (circle.impact(x, y)) {
	// Si hay colisión, oscurece el color del círculo.
	circle.fillInBlack();
      }
    }
  } 

  override function frameStarted (event:Event) : Void { }
  override function frameEnded (event:Event) : Void { }

}
